package net.yegong.matrix;

/**
 * @author cooper
 */
public class Transformer {

	public static void addEach(Matrix target, float f) {
		float[] array2 = target.array;
		for (int i = 0; i < array2.length; ++i) {
			array2[i] += f;
		}
	}

	public static void minusEach(Matrix target, float f) {
		float[] array2 = target.array;
		for (int i = 0; i < array2.length; ++i) {
			array2[i] -= f;
		}
	}

	public static void multiplyEach(Matrix target, float f) {
		float[] array2 = target.array;
		for (int i = 0; i < array2.length; ++i) {
			array2[i] *= f;
		}
	}

	public static void divideEach(Matrix target, float f) {
		float[] array2 = target.array;
		for (int i = 0; i < array2.length; ++i) {
			array2[i] /= f;
		}
	}

	public static void addEachRow(Matrix target, RowVector v) {
		if (v.nCol != target.nCol)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < target.nCol; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nRow; ++j) {
				array2[j + i * target.nRow] += tmp;
			}
		}
	}

	public static void minusEachRow(Matrix target, RowVector v) {
		if (v.nCol != target.nCol)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < target.nCol; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nRow; ++j) {
				array2[j + i * target.nRow] -= tmp;
			}
		}
	}

	public static void multiplyEachRow(Matrix target, RowVector v) {
		if (v.nCol != target.nCol)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < target.nCol; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nRow; ++j) {
				array2[j + i * target.nRow] *= tmp;
			}
		}
	}

	public static void divideEachRow(Matrix target, RowVector v) {
		if (v.nCol != target.nCol)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < target.nCol; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nRow; ++j) {
				array2[j + i * target.nRow] /= tmp;
			}
		}
	}

	public static void addEachColumn(Matrix target, ColumnVector v) {
		if (v.nRow != target.nRow)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < v.nRow; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nCol; ++j) {
				array2[i + j * v.nRow] += tmp;
			}
		}
	}

	public static void minusEachColumn(Matrix target, ColumnVector v) {
		if (v.nRow != target.nRow)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < v.nRow; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nCol; ++j) {
				array2[i + j * v.nRow] -= tmp;
			}
		}
	}

	public static void multiplyEachColumn(Matrix target, ColumnVector v) {
		if (v.nRow != target.nRow)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < v.nRow; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nCol; ++j) {
				array2[i + j * v.nRow] *= tmp;
			}
		}
	}

	public static void divideEachColumn(Matrix target, ColumnVector v) {
		if (v.nRow != target.nRow)
			throw new MatrixDimensionException();
		float[] array2 = target.array;
		for (int i = 0; i < v.nRow; ++i) {
			float tmp = v.array[i];
			for (int j = 0; j < target.nCol; ++j) {
				array2[i + j * v.nRow] /= tmp;
			}
		}
	}
}
